﻿using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Models.Common;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using UtilsCache;
using UtilsORM;
using UtilsORM.MySqlData;

////using System.Web.Script.Serialization;

namespace ServiceWeb.DBGateway
{
    public class DBIotDeviceModel
    {
        EFYihongIotContext ef = new EFYihongIotContext();
        public List<iot_device_model> getDeviceModelList()
        {
            return ef.iot_device_model.Where(c => c.m_status == 0).ToList();
        }

        public List<iot_device_model> getGateWayModelList()
        {
            return ef.iot_device_model.Where(c => c.m_status == 0 && c.m_attribute == 3).ToList();
        }
        public iot_device_model getModel(string mguid)
        {
            return ef.iot_device_model.Where(c => c.m_guid == mguid).FirstOrDefault();
        }
        public bool saveAll(IFormCollection FC, HttpReqModel httpRequest, MagerLoginInfo LoginUserInfo, iot_device_model req, HttpReqModel httpReqModel)
        {
            //先查一下有没重复提交
            iot_device_model model = ef.iot_device_model.Where(c => c.m_guid == req.m_guid).FirstOrDefault();
            iot_device_model_type model_Type = ef.iot_device_model_type.Where(c => c.t_guid == req.m_type_guid).FirstOrDefault();


            //  1执行器，2传感器，3网关,4延时
            if (model_Type.t_code == "1001" || model_Type.t_code == "1009")
            {
                req.m_attribute = 2;
            }
            else if (model_Type.t_code == "1007" || model_Type.t_code == "1004")
            {
                req.m_attribute = 1;
            }
            else if (model_Type.t_code == "1002" || model_Type.t_code == "1012")
            {
                req.m_attribute = 3;
            }
            else if (model_Type.t_code == "1008")
            {
                req.m_attribute = 4;
            }
            else
            {
                req.m_attribute = 2;
            }
            if (model == null)
            {
                //查编码
                iot_device_model getcode = ef.iot_device_model.OrderByDescending(c => c.m_id).FirstOrDefault();
                if (getcode != null)
                {
                    req.m_code = (int.Parse(getcode.m_code) + 1).ToString();
                }
                else
                {
                    req.m_code = "1001";
                }
                //写入
                req.m_add_time = DateTime.Now;
                req.m_c_guid = LoginUserInfo.userCompanyGuid;
                if (req.m_agreement_guid != null)
                {
                    req.m_agreementType = ef.iot_device_agreement.Where(c => c.a_guid == req.m_agreement_guid).FirstOrDefault().a_name;
                }

                ef.iot_device_model.Add(req);
                ef.SaveChanges();
            }
            else
            {
                model.m_attribute = req.m_attribute;
                model.m_name = req.m_name;
                model.m_type_guid = req.m_type_guid;
                model.m_remark = req.m_remark;
                model.m_value_remark = req.m_value_remark;
                model.m_brand = req.m_brand;
                model.m_parameter_demo = req.m_parameter_demo;
                model.m_command_explain = req.m_command_explain;
                model.m_agreement_guid = req.m_agreement_guid;
                model.m_parameter_demo_init = req.m_parameter_demo_init;
                model.m_status = req.m_status;
                if (req.m_agreement_guid != null)
                {
                    model.m_agreementType = ef.iot_device_agreement.Where(c => c.a_guid == req.m_agreement_guid).FirstOrDefault().a_name;
                }
                ef.iot_device_model.Update(model);
                ef.SaveChanges();
            }

            //先删除原来的
            ef.iot_device_model_file.Where(c => c.f_m_guid == req.m_guid).ExecuteDelete();
            //添加图片
            if (!string.IsNullOrEmpty(httpReqModel.fileurl))
            {
                string[] files = httpReqModel.fileurl.Split(',');
                foreach (string file in files)
                {
                    iot_device_model_file modelfile = new iot_device_model_file();
                    modelfile.f_guid = System.Guid.NewGuid().ToString();
                    modelfile.f_m_guid = req.m_guid;
                    modelfile.f_type = "info";
                    modelfile.f_url = file;
                    modelfile.f_extension = Path.GetExtension(file);
                    modelfile.f_addtime = DateTime.Now;
                    ef.iot_device_model_file.Add(modelfile);
                    ef.SaveChanges();
                }
            }
            if (!string.IsNullOrEmpty(httpReqModel.fileurlLogo))
            {
                string[] filelogos = httpReqModel.fileurlLogo.Split(',');
                foreach (string file in filelogos)
                {
                    iot_device_model_file modelfile = new iot_device_model_file();
                    modelfile.f_guid = System.Guid.NewGuid().ToString();
                    modelfile.f_m_guid = req.m_guid;
                    modelfile.f_type = "logo";
                    modelfile.f_url = file;
                    modelfile.f_extension = Path.GetExtension(file);
                    modelfile.f_addtime = DateTime.Now;
                    ef.iot_device_model_file.Add(modelfile);
                    ef.SaveChanges();
                }
            }
            if (!string.IsNullOrEmpty(FC["fileurlGuid"].ToString()))
            {
                List<ModelFileInfo> getfiles = UtilMemoryCacheHelper.get<List<ModelFileInfo>>(FC["fileurlGuid"]);
                if (getfiles == null)
                    getfiles = new List<ModelFileInfo>();

                foreach (ModelFileInfo file in getfiles)
                {
                    iot_device_model_file modelfile = new iot_device_model_file();
                    modelfile.f_guid = System.Guid.NewGuid().ToString();
                    modelfile.f_m_guid = req.m_guid;
                    modelfile.f_type = "file";
                    modelfile.f_url = file.url;
                    modelfile.f_filename = file.name;
                    modelfile.f_extension = Path.GetExtension(file.url);
                    modelfile.f_addtime = DateTime.Now;
                    ef.iot_device_model_file.Add(modelfile);
                    ef.SaveChanges();
                }

            }
            return true;
        }

    }
}
